import express from "express";
import Redis from "ioredis";
import fs from "node:fs";
const lua = fs.readFileSync("./index.lua", "utf-8");
const redis = new Redis();
const app = new express();
//限流阀
const TIME = 30; //30s之内的操作
const LIMIT = 5; //操作了5次
const KEY = "lottery"; //redis存的值
//解决跨域问题
app.use("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  next();
});
app.get("/lottery", (req, res) => {
  //lua 就是lua的脚本
  //1 代表有一个key
  //key就是接受的key
  //TIME 是 第一个参数
  //LIMIT 是 第二个参数
  redis.eval(lua, 1, KEY, TIME, LIMIT, (err, result) => {
    if (err) {
      console.log("err", err);
    }
    console.log("result", result);
    if (result === 1) {
      res.send("抽奖成功");
    } else {
      res.send("请稍后重试！");
    }
  });
});
app.listen(3000, () => {
  console.log("Server started on port 3000");
});
